package com.mxx.bu_yang.dialog;

import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mxx.basic.utils.SPUtils;
import com.mxx.bu_yang.R;
import com.mxx.bu_yang.adapter.WarehouseAdapter;
import com.mxx.bu_yang.response.LoginInfoResponse;
import com.mxx.bu_yang.response.WarehouseResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：刘江飞
 * 时间：2020/10/14  15:14
 * 邮箱：642371742Z@qq.com
 * 描述：
 **/
public class WarehouseListDialog extends CommonListDialog<WarehouseResponse> {

    public WarehouseListDialog(int widgetWidth, int location_x, int widgetHeight, int location_y, boolean transparent) {
        super(widgetWidth, location_x, widgetHeight, location_y, transparent);
    }

    /**
     * Sets layout id.
     *
     * @return the layout id
     */
    @Override
    protected int setLayoutId() {
        return R.layout.dialog_warehouse_list;
    }


    @Override
    protected BaseQuickAdapter createAdapter() {
        return new WarehouseAdapter(R.layout.adapter_common_drown_down);
    }

    @Override
    protected void loadServerData() {
        //user/operationInventory/selStorageLocation
//        int ware_id = getLoginInfo().wareId;
//        new RetrofitHelper
//                .Builder()
//                .baseUrl(NetUrlConfig.BASE_URL)
//                .appendUrl("user/operationInventory/selStorageLocation" + "?wareId=" + ware_id)
//                .build()
//                .observable(RequestEnum.METHOD_GET_NO_PARAMS)
//                .subscribe(new DefaultListObserver(true, mActivity) {
//                    @Override
//                    protected void handlerJsonArrayData(String arrayJson) {
//                        Log.i("", "");
//                        List<WarehouseResponse> list = paramsArrayJsonToList(arrayJson);
//                        ((WarehouseAdapter) mAdapter).setList(list);
//                    }
//
//                    @Override
//                    protected void onFailed(int code, String msg) {
//                        super.onFailed(code, msg);
//                    }
//                });
        List<WarehouseResponse> list = (List<WarehouseResponse>) getSerializableValue("list");
        if (list != null && !list.isEmpty()) {
            ((WarehouseAdapter) mAdapter).setList(list);
        }
    }

    private List<WarehouseResponse> paramsArrayJsonToList(String arrayJson) {
        List<WarehouseResponse> list = new ArrayList<>();
        if (arrayJson != null && !arrayJson.isEmpty()) {
            list = new Gson().fromJson(arrayJson, new TypeToken<List<WarehouseResponse>>() {
            }.getType());
        }

        return list;
    }

    private LoginInfoResponse getLoginInfo() {
        LoginInfoResponse response = null;
        String json = (String) SPUtils.get(mActivity, "login_info", "");
        if (json != null && !json.isEmpty()) {
            response = new Gson().fromJson(json, new TypeToken<LoginInfoResponse>() {
            }.getType());
        }
        return response;
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (view.getId() == R.id.tv_name) {
            if (backDataListener == null) {
                return;
            }
            WarehouseResponse response= (WarehouseResponse) adapter.getItem(position);
            backDataListener.backData(response);
            dismiss();
        }
    }
}
